﻿using UnityEngine;
using System.Collections.Generic;
using TTGame;
using CinemaDirector;
using TTGameEngine;

/*
 * AttackFrameTigger
 * 触发器-攻击框
 *
 * 触发后，会产生攻击框
 * 本质上是产生一个法球。
 *
 * 由于是攻击框，只有一个简单的以人物为圆心的各种形状的碰撞检测。
 *
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/18/2017 7:44:56 PM
 */
[CutsceneItemAttribute("Bullet", "LTAttackFrame Trigger", CutsceneItemGenre.ActorItem)]
public class AttackFrameTrigger : CinemaActorEvent {
    public float m_lifeTime;       //持续时间 0表示只触发一次
    public ColliderInfo m_colliderInfo; //碰撞信息
    public string bulletPrefab = "";
    //从Skill属性获得，服务器赋值
    //public int maxHarmCount = 1;  //
    [Tooltip("二级子弹Prefab")]
    public string followUpBulletPrefab = "";
    [Tooltip("不造成伤害。注意，依然会碰撞，会触发技能效果")]
    public bool isNoHarm = false;
    [Tooltip("存在多个碰撞对象时，优先选择的策略")]
    public TargetStrategy m_targetStrategy = TargetStrategy.HeroSeatPrior_MonsterNearest;
    [SerializeField, Tooltip("子弹消耗")]
    public int m_bulletConsume = 1;

    public override void Trigger(GameObject actor) {
#if UNITY_EDITOR
        if(Application.isPlaying == false) {
            return;
        }
#endif
        SkillTL skill = this.GetSkill();
        //fixed:停止技能脚本时，脚本上所有剩余的触发器都会触发一次。
        if(skill.IsStopped()) { return; }

        Puppet obj = actor.GetComponent<Puppet>();
        if(obj == null) { return; }

        AttackFrame atkFrame = MagicBallManager.Share().CreateMBWithPrefab(bulletPrefab) as AttackFrame;
        if(atkFrame == null) {
            DebugUtil.LogErrorFormat("[AttackFrameTrigger] 子弹预设[{0}]上没有获取到AttackFrame脚本. obj:[{1}]", bulletPrefab, actor.name);
            return;
        }

        skill.RegistBulletLogicHandler(atkFrame);
        skill.BulletCount -= m_bulletConsume;
        atkFrame.m_bulletConsume = m_bulletConsume;

        SkillData skillData = skill.m_skillData;
        atkFrame.Init(obj, skill.SkillID, skill.SkillCastID, m_lifeTime, m_colliderInfo);
        atkFrame.InitDamageInfo(skillData.dmgRatio, skillData.dmgAdd, skillData.crit, obj.FetterAtkRate);
        atkFrame.SetIsNoHarm(isNoHarm);
        atkFrame.SetEffectLayerID(obj.GetEffectLayerID());
        atkFrame.SetMaxColliderCount(skillData.maxHitTarget);
        atkFrame.SetFollowUpBullet(followUpBulletPrefab, skill.SkillLogicHandler);
        atkFrame.SetColliderTargetStrategy(m_targetStrategy);
        atkFrame.AddBuff(skill.SkillLogicHandler.BuffIDs);
        atkFrame.EmitSelf();
    }

    void OnDrawGizmos() {
        if(Application.isPlaying == false) {
            Tools.DrawColliderInfo(transform, m_colliderInfo);
        }
    }
}
